import { _decorator, Component, debug, DebugMode, director, game, Game, Node, sys, Profiler, profiler, cclegacy, Settings, settings } from 'cc';

import { platform } from '../platform/platform';
import { platform_Wx } from '../platform/platform_Wx';
import { platform_Dy } from '../platform/platform_Dy';
import { UIManager } from './UIManager';
import EvtManger from '../tools/EvtManger';
import { resManger } from '../tools/ResManger';
import { GameData } from '../system/GameData';
import { SoundManager } from './SoundManger';
import { LevelChallenge } from '../pop/LevelChallenge';
const { ccclass, property } = _decorator;


export const popLevelMap = new Map();
//预设弹窗名对应的弹窗等级
export enum AppState {
    default,
    init,
    ready,
    game
}

export enum EventName {
    coninIntoBag = "coninIntoBag",
    appStateChange = "appStateChange",
    monsterDeath = "monsterDeath", //怪物死亡与走到目标消失都会调用 但是只有死亡会触发加钱和荣誉
    monsterRefreshChange = "monsterRefreshChange",
    stageChange = "stageChange",
    goldUpdate = "goldUpdate",
    mainGunLevelUp = "mainGunLevelUp",
    secondGunLevelUp = "secondGunLevelUp",
    resetGun = "resetGun",
    upgradeSecondGun = "upgradeSecondGun",
    setTarget = "setTarget",
    lockMonsterByTouch = "lockMonsterByTouch",
    shootByTouch = "shootByTouch",
    automaticUpgrade = "automaticUpgrade",
    addGold = "addGold",
    addHonor = "Honor",
    showGift = "showGift",
    hideGift = "hideGift",
    receiveGift = "receiveGift",
    showBossHp = "showBossHp",
    bossDeath = "bossDeath",
    tasteGun = "tasteGun",
    dropCoins = "dropCoins",
    dropGiftGold = "dropGiftGold",
    dropSkin = "dropSkin",
    dropSkinTriple = "dropSkinTriple",
    dropMaterial = "dropMaterial",
    dropPearls = "dropPearls",
    unlockSkin = "unlockSkin",
    updateUsingSkin = "updateUsingSkin",
    dropCoinsByPop = "dropCoinsByPop",
    secondGuide = "secondGuide",
    normalMosterRecovery = "normalMosterRecovery",
    levelChallenge = "levelChallenge",
    normalChanllenge = "normalChanllenge",
    levelModeMoveFinish = "levelModeMoveFinish",
    exitLevel = "exitLevel",
    levelGift = "levelGift",
    doExitLevel = "doExitLevel",
    updateShop = "updateShop",
    updatePearls = "updatePearls",
    addPearls = "addPearls",
    randomLvUp = "randomLvUp",
    mainGuide = "mainGuide"
}

export type level_monster_Refresh_config = {
    stageID: number,
    minKillMonsters: number,
    maxKillMonsters: number,
    refreshInterval: number,
    monsterPool: number[][],
    maxMonster: number,
    BloodVolumeRatio: number,
    speedRatio: number,
    lineRoute: number[],
    monsterMovement: number;
    dropGoldRatio: number,
    dropHonorRatio: number,
    stageAverageDropGold: number,
    stageAverageDropHonor: number,
    bgImage: string,
    bgSize: number[];
    stageAverageHp: number,
    LimitedTimeAwardGold: number
}

export type monster_Refresh_config = {
    stageID: number,
    minKillMonsters: number,
    maxKillMonsters: number,
    refreshInterval: number,
    monsterPool: number[][],
    maxMonster: number,
    BloodVolumeRatio: number,
    speedRatio: number,
    lineRoute: number[],
    monsterMovement: number;
    dropGoldRatio: number,
    dropHonorRatio: number,
    stageAverageDropGold: number,
    stageAverageDropHonor: number,
    bgImage: string,
    bgSize: number[];
    stageAverageHp: number,
    LimitedTimeAwardGold: number,
    levelReward: number[]
}

export type moveLine_config = {
    lineID: number,
    leftPosition: number[],
    rightPosition: number[];
    levelLine: [][],
}

export type monster_base_config = {
    MonsterID: number,
    baseBlood: number,
    baseGold: number,
    baseHonor: number,
    baseSpeed: number,
    monsterRes: string,
    deadAni: string,
    monsterSize: number[],
    deathAudio: string,
    runAniSpeed: number,
    deathAniSpeed: number
}

export type MainGun__config = {
    mainGunID: number,
}

export type MainGun_Item = {
    attributeType: number,
    attributeNum: number,
    UpgradeConsumption: number,
    upgradation: number,
    bullet: number,
    mainGunRes: string,
    mainGunSize: number[],
    mainGunBottomSize: number[],
    mainGunBottomRes: string,
    gunBottomPosition: number[],
    gunFirePosition: number[],
    gunFireSize: number[],
    gunFireAniSpeed: number,
}

export type bullet_config = {
    bulletID: number,
    moveSpeed: number,
    rightPosition: string,
    beizhu: string,
    bulletSize: number[],
    bulletFlash: string,
    bulletAudio: string,
    firePosition: number
}

export type current_config = {
    fieldName: string,
    value: string
}

export enum configKey {
    maxAtkSpeed
}

export type secondaryGun_config = {
    levelID: number,
    levelNum: number,
    aggressivity: number,
    attackSpeed: number,
    upgradation: number
}

export type secondGun_specialSkill_config = {
    skill: number,
    skillType: number,
    skillNum: number[],
    skillCD: number,
    secondGunRes: string,
    secondGunBottomRes: string,
    secondGunSize: number[],
    secondGunBottomSize: number[],
    secondGunTDRes: string,
    skillDescribe: string,
    RandomNum: number,
    skillQuality: number,
    secondGunName: string,
    skillAni: string,
    skillSpeed: number,
    bulletRes: number,
    anchorPoint: number[],
    gunPosition: number[],
    attrPanelPosition: number[],
    skillFireTime: number,
    skillAniSpeed: number,
    gunFirePosition: number[],
    gunFire: string,
    gunFireAniSpeed: number,
    gunFireSize: number,
    fireAudio: string,
    GunRank: string,
    aniAudio: string,
    firePosition: number,
    skillBullet: number,
    atkAdd: string
}

export enum PopView {
    usingAgain = "usingAgain",
    setting = "setting",
    unlockSecondGun = "unlockSecondGun",
    secondGunUi = "secondGunUi",
    autoFight = "autoFight",
    systemMessage = "systemMessage",
    getSecondGun = "getSecondGun",
    temporaryGift = "temporaryGift",
    secondGunList = "secondGunList",
    worldRank = "rank",
    getSkinDebris = "getSkinDebris",
    skin = "skin",
    sevenSignIn = "sevenSignIn",
    levelChallenge = "levelChallenge",
    exitGame = "exitGame",
    levelChallengeResult = "levelChallengeResult",
    dailyTurntable = "dailyTurntable",
    secretShop = "secretShop",
    secondGunRandom = "secondGunRandom",
    secondGunChoose = "secondGunChoose"
}

export type secondGun_unlock_config = {
    positionId: number,
    positionNum: number,
    positionCoordinate: number[],
    unlockType: number,
    unlockNum: number,
    unlockText: string
}

export type tasteMainGun_config = {
    tasteId: number,
    dropPrefab: string,
    usingAgainNode: string,
    gunRes: string,
    gunBottomRes: string,
    gunSize: number[],
    gunBottomSize: number[],
    gunAnchorPoint: number[],
    gunBottomPosition: number[],
    bulletId: string,
    aniRes: string,
    aniAudio: string,
    aniSize: number[],
    beizhu: string,
    skillRange: number,
    gunFirePosition: number[],
    gunFireSize: number[],
    gunFireAniSpeed: number,
    skillAniSpeed: number
}

export type skin_config = {
    skinId: number,
    skinName: string,
    unlockAttrType: number,
    unlockNum: number,
    unlockValue: string,
    mainGunSkinRes: string,
    unlockNeedDebris: number,
    debrisDropStage: number[],
    debrisDrioValue: string,
    dropType: number,
    debrisDropRate: number[]
}

export type prop_config = {
    propID: number,
    propType: number,
    propNum: number,
    propIcon: string,
    IconSize: number[],
    propName: string,
    LabelMultiple: number
}

export type turntable_config = {
    turntableId: number,
    rewardPosition: number,
    rewardProp: number,
    rewardWeight: number,
    endAngle: number
}

export enum ConfigKey {
    maxAtkSpeed = "maxAtkSpeed",
    randomSecondGunSpend = "randomSecondGunSpend",
    color001 = "color001",
    color002 = "color002",
    color003 = "color003",
    color004 = "color004",
    color005 = "color005",
    color006 = "color006",
    buySecondGun = "buySecondGun",
    secondGunReset = "secondGunReset",
    bossLine = "bossLine",
    bossStopTime = "bossStopTime",
    bossRefreshChance = "bossRefreshChance",
    bossRefreshRegular = "bossRefreshRegular",
    bossRefreshRandom = "bossRefreshRandom",
    giftRefreshRegular = "giftRefreshRegular",
    giftRefreshRandom = "giftRefreshRandom",
    giftRefreshChance = "giftRefreshChance",
    giftStopTime = "giftStopTime",
    tasteGunCountDown = "tasteGunCountDown",
    tasteGunUsingCountDown = "tasteGunUsingCountDown",
    tasteGunVideoTime = "tasteGunVideoTime",
    bgmVolume = "bgmVolume",
    gunVolume = "gunVolume",
    monsterVolume = "monsterVolume",
    buffTime = "buffTime",
    defaultUserName = "defaultUserName",
    chapingShow = "chapingShow",
    AtkAddText = "AtkAddText",
    goldAddText = "goldAddText",
    autoAtkTimesText = "autoAtkTimesText",
    signInReset = "signInReset",
    autoAtkUnlock = "autoAtkUnlock",
    freeRandomText = "freeRandomText",
    sevenSign = "sevenSign",
    levelChallengeUnlock = "levelChallengeUnlock",
    levelChallengeFreeTimes = "levelChallengeFreeTimes",
    levelChallengeVideoTimes = "levelChallengeVideoTimes",
    levelHp = "levelHp",
    bossId = "bossId",
    turntableFreeTimes = "turntableFreeTimes",
    turntableVideoTimes = "turntableVideoTimes",
    secretShopFreeTimes = "secretShopFreeTimes",
    secretShopVideoTimes = "secretShopVideoTimes",
    randomGunSpend = "randomGunSpend",
    grayGunAtkProportion = "grayGunAtkProportion",
    greenGunAtkProportion = "greenGunAtkProportion",
    blueGunAtkProportion = "blueGunAtkProportion",
    purpleGunAtkProportion = "purpleGunAtkProportion",
    yellowGunAtkProportion = "yellowGunAtkProportion",
    redGunAtkproPortion = "redGunAtkproPortion",
    videoCutTime = "videoCutTime",
    propEnough = "propEnough",
    propFail = "propFail",
    stageGuild = "stageGuild"
}

export type signIn_config = {
    signId: number,
    signReward: number,
    debrisID: number,
    rewardNum: number,
    rewardValue: string,
    replaceType: number,
    replaceRewardNum: number,
    replaceValue: string,
    signRank: number,
}

export type secretShop_config = {
    shopId: number,
    rewardProp: number,
    rewardWeight: number,
    propName: string,
    LabelMultiple: number
}

export type gunRandomSpend_config = {
    probabilityLevelId: number,
    level: number,
    upgradeSpend: number[][],
    upgradeCountDown: number,
}

export type gunRandomLevel_config = {
    probabilityId: number,
    counterGun: number,
    level_1: number,
    level_2: number,
    level_3: number,
    level_4: number,
    level_5: number,
}

export type buildingPlatform_config = {
    LevelId: number,
    buildLevel: number,
    randomGunLevel: [],
    randomLevelValue: string,
    randomWeight: number[][],
    upgradeConsume: number,
    promotion: number
}

export type gunResolve_config = {
    resolveId: number,
    gunQuality: number,
    resolveGet: number[]
}

popLevelMap.set(PopView.unlockSecondGun, 0);
popLevelMap.set(PopView.secondGunUi, 0);
popLevelMap.set(PopView.usingAgain, 0)
popLevelMap.set(PopView.autoFight, 0);
popLevelMap.set(PopView.temporaryGift, 0);
popLevelMap.set(PopView.getSecondGun, 0);
popLevelMap.set(PopView.setting, 0);
popLevelMap.set(PopView.worldRank, 0);
popLevelMap.set(PopView.sevenSignIn, 0);
popLevelMap.set(PopView.skin, 0);
popLevelMap.set(PopView.getSkinDebris, 0);
popLevelMap.set(PopView.levelChallenge, 0);
popLevelMap.set(PopView.exitGame, 0);
popLevelMap.set(PopView.levelChallengeResult, 0);
popLevelMap.set(PopView.secondGunList, 1);
popLevelMap.set(PopView.systemMessage, 2);
popLevelMap.set(PopView.dailyTurntable, 0);
popLevelMap.set(PopView.secondGunRandom, 1);
popLevelMap.set(PopView.secondGunChoose, 1);
popLevelMap.set(PopView.secretShop, 0);


@ccclass('AppControll')
export class AppControll extends Component {
    private mUi: UIManager = null;
    private mState: AppState = AppState.default;
    static Platform: platform = null;
    static isDebug = false;
    static ShelvedTime = 0;
    static IsShowInitInterstitial = false;

    onLoad() {
        this.RegisteredEvent();
        this.mUi = this.node.parent.getChildByName("Ui").getComponent(UIManager);
        this.InitPlatform();
        this.StateProcessing();
    }

    RegisteredEvent() {
        EvtManger.on(EventName.appStateChange, this, this.Receiver);
    }

    Receiver(state: AppState) {
        switch (state) {
            case AppState.ready:
                this.StateToReady();
        }
    }

    StateToReady() {
        if (this.mState == AppState.default) {
            this.mState = AppState.ready;
        }
        this.StateProcessing();
    }

    InitPlatform() {
        switch (sys.platform) {
            case sys.Platform.WECHAT_GAME:
                AppControll.Platform = new platform_Wx();
                break;
            case sys.Platform.BYTEDANCE_MINI_GAME:
                AppControll.Platform = new platform_Dy();
                break;
            default:
                AppControll.Platform = new platform_Wx();
                break;
        }
        
        AppControll.Platform.SendBuryPoint(100001);
       
    }

    Registered() {
    }

    onDestroy() {
    }

    //load.onload 
    StateProcessing() {
        switch (this.mState) {
            case AppState.default:
                GameData.InitPlayData();
                break;
            case AppState.ready:
                this.mUi.ShowFight();
                GameData.playerData.challengeCount = GameData.GetCurlevelChallengeFreeTimes();
                GameData.playerData.adChallengeCount = GameData.GetCurlevelChallengeAdTimes();
                GameData.playerData.turntableFreeTimes = GameData.GetTurnFreeTimes();
                GameData.playerData.turntableVideoTimes = GameData.GetTurnADTimes();
                GameData.playerData.secretShopFreeTimes = GameData.GetSecretShopFreeTimes();
                GameData.playerData.secretShopVideoTimes = GameData.GetSecretShopVideoTimes();
                GameData.InitSecretShop();
                GameData.playerData.secretShopReward = GameData.GetSecretShopLt();
                if (GameData.playerData.lastTime == null) {
                    GameData.SavePlayData();
                }
                if (GameData.playerData.musicSwitch) {
                    SoundManager.Instance<SoundManager>().PlayBgm();
                }
                break;
        }

    }
}

